package BinaryTree;
/*
 * 二叉树的最近公共祖先
 */
import BinaryTree.init.TreeNode;

public class lc236 {
    public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
        if (root == null) return root;
        if (root == p || root == q) return root;
        TreeNode lef = lowestCommonAncestor(root.left, p, q);
        TreeNode rig = lowestCommonAncestor(root.right, p, q);
        if (lef != null && rig != null) return root;
        if (lef != null && rig == null) return lef;
        if (lef == null && rig != null) return rig;
        return null;
    }

}
